//
//  MovieMapViewController.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/11/4.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "MovieMapViewController.h"
#import "MovieDetailViewController.h"
#import "RootCollectionViewCell.h"
#import "MovieListDB.h"
@interface MovieMapViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *movieCollectionView;

@end

@implementation MovieMapViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.movieMap = [[MovieListDB shareDataBase]selectSutdent];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 12, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.movieCollectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds]collectionViewLayout:flowLayout];
    [self.view addSubview:self.movieCollectionView];
    [_movieCollectionView release];
    
    self.movieCollectionView.delegate = self;
    self.movieCollectionView.dataSource = self;
    [self.movieCollectionView registerClass:[RootCollectionViewCell class] forCellWithReuseIdentifier:@"liuxin"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.movieMap.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *idetifer = @"liuxin";
    RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idetifer forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/256.0 green:(arc4random()%256)/256.0 blue:(arc4random()%256)/256.0 alpha:1];
    cell.mov = [self.movieMap objectAtIndex:indexPath.item];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    MovieList *movie = [self.movieMap objectAtIndex:indexPath.item];
    movieDetailVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end